var appInstance = getApp(), dataApi = require("../../provider/dataApi.js"), pipe = require("../../provider/pipe.js");
let interstitialAd = null;

Page({
    data: {
        isLoading: !0,
        isCanAnswer: !1,
        statusBarHeight: "",
        titleBarHeight: "",
        bannerList: [],
        noticeList: [],
        lastId: 0,
        totalQuestions: 0,
        bannerHeight: "",
        isCan: "",
        adInfo: {},
        adHave: 2,
        icons: {},
        countdown: {},
        countdownTime: 0,
        countDay: {},
        rate: {},
        activationShow: !1,
        setCDKey: ""
    },
    registerFormSubmit: function(event) {
        wx.getStorageSync("uid") && dataApi.getFormid({
            uid: wx.getStorageSync("uid"),
            formid: event.detail.formId
        }).then(function(response) {}).catch(function(error) {});
    },
    onLoad: function(options) {
        this.setData({
            statusBarHeight: appInstance.globalData.statusBarHeight,
            titleBarHeight: appInstance.globalData.titleBarHeight
        });
    },
    goBanner: function(event) {
        wx.getStorageSync("uid") || wx.navigateTo({
            url: "../login/login"
        }), "1" == (event = appInstance.globalData.item = event.currentTarget.dataset.item).type ? ("1" == (event = event.link) && wx.navigateTo({
            url: "/pages/my-medal/my-medal"
        }), "2" == event && wx.switchTab({
            url: "/pages/error/error"
        }), "3" == event && wx.switchTab({
            url: "/pages/ranking/ranking"
        }), "4" == event && wx.switchTab({
            url: "/pages/mine/mine"
        }), "5" == event && wx.navigateTo({
            url: "/pages/exam-desc/exam-desc"
        }), "6" == event && wx.navigateTo({
            url: "/pages/integral-mall/integral-mall"
        }), "7" == event && wx.navigateTo({
            url: "/pages/vip/vip"
        })) : "2" == event.type ? wx.navigateTo({
            url: "../webView/webView"
        }) : "3" == event.type && (appInstance.globalData.id = event.link, wx.navigateTo({
            url: "/pages/article-info/article-info?type=article"
        }));
    },
    goLx: function() {
        var isCanAnswer = this.data.isCanAnswer;
        appInstance.globalData.op = "sequence", wx.getStorageSync("uid") ? isCanAnswer ? (appInstance.globalData.id = "", 
        wx.navigateTo({
            url: "/pages/quesBankType/quesBankType"
        })) : this.testInfo() : wx.navigateTo({
            url: "../login/login"
        });
    },
    goExam: function() {
        var isCanAnswer = this.data.isCanAnswer;
        wx.getStorageSync("uid") ? isCanAnswer ? wx.navigateTo({
            url: "../exam-desc/exam-desc"
        }) : this.testInfo() : wx.navigateTo({
            url: "../login/login"
        });
    },
    bindWechat: function() {
        wx.showModal({
            title: "绑定微信",
            content: "需要绑定微信才能进行下一步操作,点击确定去绑定",
            showCancel: !0,
            confirmText: "确定",
            success: function(response) {
                console.log(response), response.confirm && wx.navigateTo({
                    url: "../bindWechat/bindWechat"
                });
            }
        });
    },
    testInfo: function() {
        wx.showModal({
            title: "完善信息",
            content: "答题前需要完善姓名和手机号信息",
            showCancel: !0,
            confirmText: "去完善",
            success: function(response) {
                console.log(response), response.confirm && wx.navigateTo({
                    url: "../my-info/my-info"
                });
            }
        });
    },
    golianX: function(event) {
        event = event.currentTarget.dataset.item;
        var doId = Number(event.doid), types = event.types, title = event.title, isCanAnswer = this.data.isCanAnswer;
        if (wx.getStorageSync("uid")) if (isCanAnswer) if (console.log(event), appInstance.globalData.id = "", 2 == types) 0 == event.is_can ? wx.navigateTo({
            url: "/pages/classification/classification?type=1&id=" + event.gpid
        }) : (appInstance.globalData.op = "special", appInstance.globalData.id = event.library_id, wx.navigateTo({
            url: "/pages/practice/practice"
        })); else switch (appInstance.globalData.title = title, doId) {
          case 1:
            wx.navigateTo({
                url: "../classification/classification"
            });
            break;

          case 2:
            wx.navigateTo({
                url: "/pages/quesBankType/quesBankType?type=1"
            });
            break;

          case 3:
            appInstance.globalData.op = "randoms", wx.navigateTo({
                url: "/pages/quesBankType/quesBankType"
            });
            break;

          case 4:
            appInstance.globalData.op = "collection_question", wx.navigateTo({
                url: "../practice/practice"
            });
            break;

          case 5:
            wx.switchTab({
                url: "../error/error"
            });
            break;

          case 6:
            appInstance.globalData.op = "notdone", wx.navigateTo({
                url: "/pages/quesBankType/quesBankType"
            });
            break;

          case 7:
            appInstance.globalData.type = 1, wx.navigateTo({
                url: "../knowledgePoints/knowledgePoints"
            });
            break;

          case 8:
            appInstance.globalData.type = 2, wx.navigateTo({
                url: "../knowledgePoints/knowledgePoints"
            });
        } else this.testInfo(); else wx.navigateTo({
            url: "../login/login"
        });
    },
    getCode: function() {
        wx.getStorageSync("uid") ? this.setData({
            activationShow: !0
        }) : wx.navigateTo({
            url: "../login/login"
        });
    },
    onClose: function() {
        this.setData({
            activationShow: !1
        });
    },
    onChange: function(event) {
        this.setData({
            setCDKey: event.detail
        });
    },
    onConfirm: function() {
        var self = this;
        this.data.setCDKey ? dataApi.setCDKey({
            uid: wx.getStorageSync("uid"),
            code: this.data.setCDKey
        }).then(function(response) {
            console.log(response), wx.showToast({
                icon: "none",
                title: response.message
            }), self.setData({
                setCDKey: ""
            });
        }).catch(function(error) {
            console.log(error), wx.showToast({
                icon: "none",
                title: "该激活码不可用"
            });
        }) : wx.showToast({
            icon: "none",
            title: "请输入激活码"
        });
    },
    queryAd: function() {
        var self = this;
        dataApi.advert({
            op: "home"
        }).then(function(response) {
            console.log(response), self.setData({
                adInfo: response.data.info,
                adHave: response.data.ad_have
            });
            if(wx.createInterstitialAd){
              interstitialAd = wx.createInterstitialAd({ adUnitId: self.data.adInfo.homeId })
              interstitialAd.onLoad(() => {
                console.log('onLoad event emit')
              })
              interstitialAd.onError((err) => {
                console.log('onError event emit', err)
              })
              interstitialAd.onClose((res) => {
                console.log('onClose event emit', res)
              })
            }
        }).catch(function(error) {
            console.log(error);
        });
    },
    onShow: function() {
        this.getuserinfo(), this.totalqNum(), this.indexData(), this.queryAd(), this.versionUpdating();
    },
    versionUpdating: function() {
        if (wx.canIUse("getUpdateManager")) {
            var updateManager = wx.getUpdateManager();
            updateManager.onCheckForUpdate(function(response) {
                console.log("onCheckForUpdate====", response), response.hasUpdate && (console.log("res.hasUpdate===="), 
                updateManager.onUpdateReady(function() {
                    wx.showModal({
                        title: "更新提示",
                        content: "新版本已经准备好，是否重启应用？",
                        showCancel: !1,
                        confirmText: "确认",
                        success: function(response) {
                            console.log("success====", response), response.confirm && (updateManager.applyUpdate(), wx.clearStorageSync());
                        }
                    });
                }), updateManager.onUpdateFailed(function() {
                    wx.showModal({
                        title: "已经有新版本了哟~",
                        content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~"
                    });
                }));
            });
        }
    },
    goNotice: function(event) {
        wx.getStorageSync("uid") || wx.navigateTo({
            url: "../login/login"
        }), appInstance.globalData.id = event.currentTarget.dataset.id, wx.navigateTo({
            url: "../article-info/article-info?type=notice"
        });
    },
    indexData: function() {
        var self = this, userId = 0;
        wx.getStorageSync("uid") && (userId = wx.getStorageSync("uid")), dataApi.indexData({
            uid: userId
        }).then(function(response) {
            console.log(response), self.setData({
                bannerList: response.data.banner,
                noticeList: response.data.notice,
                bannerHeight: response.data.banner_height,
                shareTitle: response.data.share_title,
                icons: response.data.icons,
                countdown: response.data.countdown,
                countDay: response.data.count_day,
                rate: response.data.rate,
                isLoading: !1
            }), console.log(response.data.countdown), self.djsFun(response.data.countdown.countdowntime);
        }).catch(function(error) {
            console.log(error);
        });
    },
    djsFun: function(countdownTime) {
        var currentTime = 0;
        0 < 1e3 * countdownTime - new Date().getTime() && (currentTime = pipe.formatDateDayTimehour(1e3 * countdownTime)), this.setData({
            countdownTime: currentTime
        });
    },
    totalqNum: function() {
        var self = this;
        dataApi.totalqNum({
            uid: wx.getStorageSync("uid")
        }).then(function(response) {
            console.log(response), self.setData({
                lastId: Number(response.data.last_id),
                totalQuestions: response.data.total,
                isCan: response.data.is_can
            });
        }).catch(function(error) {
            console.log(error);
        });
    },
    getuserinfo: function() {
        var self = this;
        dataApi.userinfo({
            uid: wx.getStorageSync("uid"),
            op: "getinfo"
        }).then(function(response) {
            console.log(response);
            var userInfo = response.data.info;
            wx.setStorageSync("userinfo", userInfo), wx.setStorageSync("http", response.data.http);
            var isShowKey = !1;
            1 == userInfo.is_ok && (isShowKey = !0), self.setData({
                isCanAnswer: isShowKey,
                isShowKey: response.data.app_id
            });
        }).catch(function(error) {
            console.log(error);
        });
    },
    onShareAppMessage: function() {}
});